package com.app.controller.backstage;

import com.app.model.AdminUserModel;
import com.app.model.AreaBaseModel;
import com.app.model.IncubatorsArticleModel;
import com.app.model.IncubatorsModel;
import com.app.service.AdminUserService;
import com.app.service.AreaBaseService;
import com.app.service.IncubatorsArticleService;
import com.app.service.IncubatorsService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.IncubatorsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: 专家
 * @Author: N
 * @Date: 2022/10/29 15:26
 */
@RestController
@RequestMapping("/")
public class IncubatorsBackstageController {
    @Autowired
    IncubatorsService incubatorsService;
    @Autowired
    AreaBaseService areaBaseService;
    @Autowired
    IncubatorsArticleService incubatorsArticleService;

    @Autowired
    AdminUserService adminUserService;

    /**
     *获取孵化基地列表
     * @param model
     * @return
     */
    @GetMapping("/getAreaBaseList")
    public Result<Object> getAreaBaseList(AreaBaseModel model){
        List<AreaBaseModel> list = areaBaseService.getListByModel(model);
        return Result.success(list);
    }

    /**
     *获取孵化基地(单个)
     * @param id
     * @return
     */
    @GetMapping("/getAreaBase")
    public Result<Object> getAreaBase(Integer id){
        AreaBaseModel model = areaBaseService.getModelById(id);
        return Result.success(model);
    }

    @RequestMapping(value = "editAreaBase", method= RequestMethod.POST)
    public Result<Object> editAreaBase(HttpServletRequest request, @RequestBody AreaBaseModel model){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        if(model.getId()!=null&&model.getId()>0)
        {
            areaBaseService.update(model);
        }else{
            model.setCrtTime(CommonHelp.getNowTimestamp().toString());
            areaBaseService.insert(model);
        }
        return Result.success("操作成功");
    }

    @RequestMapping(value = "editIntroduce", method= RequestMethod.POST)
    public Result<Object> editIntroduce(HttpServletRequest request, @RequestBody AreaBaseModel model){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }

        if(model.getId()!=null&&model.getId()>0)
        {
            areaBaseService.update(model);
        }else{
            model.setCrtTime(CommonHelp.getNowTimestamp().toString());
            areaBaseService.insert(model);
        }
        return Result.success("操作成功");
    }

    /**
     *获取孵化基地列表
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/getIncubatorsPage")
    public Result<Object> getIncubatorsPage(HttpServletRequest request, IncubatorsModel model){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        IncubatorsVo vo = incubatorsService.getPageByModel(model);
        return Result.success(vo);
    }

    @GetMapping("/getBsIncubatorsById")
    public Result<Object> getBsIncubatorsById(HttpServletRequest request, Integer id){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        IncubatorsModel model = incubatorsService.getModelById(id);
        return Result.success(model);
    }
    @GetMapping("/delBsIncubators")
    public Result<Object> delBsIncubators(HttpServletRequest request, Integer id){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        incubatorsService.delete(id);
        //删除子表数据
        IncubatorsArticleModel incubatorsArticleModel = incubatorsArticleService.getModelByFather(id,1);
        if(incubatorsArticleModel!=null)
        {
            incubatorsArticleService.delete(incubatorsArticleModel.getId());
        }
        IncubatorsArticleModel incubatorsArticleModel2 = incubatorsArticleService.getModelByFather(id,2);
        if(incubatorsArticleModel2!=null)
        {
            incubatorsArticleService.delete(incubatorsArticleModel2.getId());
        }

        return Result.success("操作成功");
    }

    @GetMapping("/getBsIncubatorsArticleByFatherId")
    public Result<Object> getBsIncubatorsArticleByFatherId(HttpServletRequest request, Integer fatherId,Integer type){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }

        IncubatorsArticleModel model = incubatorsArticleService.getModelByFather(fatherId,type);
        return Result.success(model);
    }


    @RequestMapping(value = "editIncubators", method= RequestMethod.POST)
    public Result<Object> editIncubators(HttpServletRequest request, @RequestBody IncubatorsModel model){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        if(model.getId()!=null&&model.getId()>0)
        {
            incubatorsService.update(model);
        }else{
            model.setCrtTime(CommonHelp.getNowTimestamp().toString());
            incubatorsService.insert(model);
        }
        return Result.success("操作成功");
    }


    @RequestMapping(value = "editIncubatorsArticle", method= RequestMethod.POST)
    public Result<Object> editIncubatorsArticle(HttpServletRequest request, @RequestBody IncubatorsArticleModel model){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }

        if(model.getId()!=null&&model.getId()>0)
        {
            incubatorsArticleService.update(model);
        }else{
            model.setCrtTime(CommonHelp.getNowTimestamp().toString());
            incubatorsArticleService.insert(model);
        }
        return Result.success("操作成功");
    }

}
